package cn.wsn2018.tools.service;

import cn.wsn2018.tools.common.CommonValue;
import cn.wsn2018.tools.dao.LogsInfoDao;
import cn.wsn2018.tools.model.Log.ErrorLogInfo;
import cn.wsn2018.tools.model.Log.LogsInfo;
import cn.wsn2018.tools.util.DateUtil;
import cn.wsn2018.tools.util.JsonUtil;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * @author gmz.
 * @date 2018/12/14.
 * @time 20:01
 */
@Service
public class LogsInfoService {

    @Autowired
    private LogsInfoDao logsInfoDao;

    /**
     * 根据用户名查询才用户所有日志信息
     * @param username
     * @return
     */
/*    public String findAllLogsByName(String username){
        if(username == null){
            return JsonUtil.failedJson(CommonValue.JSON_INPUT_ERROR);
        }
        List<LogsInfo> list = logsInfoDao.listLogsInfoByUser(username);
        String json = JSONArray.fromObject(list).toString();
        return json;
    }*/

    /**
     * 查询所有操作员、第X页的日志信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    public String findLogsByPageNum(String pageNum,String pageSize){
        if((pageNum == null || pageNum.isEmpty())||(pageSize == null || pageSize.isEmpty())){
            return JsonUtil.failedJson(CommonValue.JSON_INPUT_ERROR);
        }
        int pageNumber = Integer.parseInt(pageNum);
        int size = Integer.parseInt(pageSize);
        //查询指定分页的内容
        List<LogsInfo> list = logsInfoDao.listLogsByPageNum(pageNumber,size);
        JSONArray json = JSONArray.fromObject(list);
        if(json.isEmpty() || json == null){
            return JsonUtil.failedJson(CommonValue.JSON_QUERY_FAILED);
        }else {
            System.out.println(json);
            return JsonUtil.successJson(CommonValue.JSON_QUERY_SUCCESS, null, json);
        }
    }

    /**
     * 查询某一操作员、第X页的日志信息
     * @param username
     * @param pageSize
     * @param pageNum
     * @return
     */
    public String findLogsByPgNumAndName(String username,String pageNum,String pageSize){
        if((username == null || username.isEmpty()) || (pageNum == null || pageNum.isEmpty())||(pageSize == null || pageSize.isEmpty())){
            return JsonUtil.failedJson(CommonValue.JSON_INPUT_ERROR);
        }
        int pageNumber = Integer.parseInt(pageNum);
        int size = Integer.parseInt(pageSize);
        //查询指定分页的内容
        List<LogsInfo> list = logsInfoDao.listLogsInfoByUser(username,pageNumber,size);
        JSONArray json = JSONArray.fromObject(list);
        if(json.isEmpty() || json == null){
            return JsonUtil.failedJson(CommonValue.JSON_QUERY_FAILED);
        }else {
            System.out.println(json);
            return JsonUtil.successJson(CommonValue.JSON_QUERY_SUCCESS, null, json);
        }
    }

    /**
     * 根据时间范围、操作员查询某一页日志信息
     * @param username
     * @param pageNum
     * @param pageSize
     * @param fromTime
     * @param toTime
     * @return
     */
    public String findLogsByTimeAndName(String username,String pageNum,String pageSize,String fromTime,String toTime){
        if((username == null || username.isEmpty())||(fromTime == null || fromTime.isEmpty()) ||(toTime == null ||
                toTime.isEmpty()) ||(pageNum == null || pageNum.isEmpty()) || (pageSize == null || pageSize.isEmpty())){
            return JsonUtil.failedJson(CommonValue.JSON_INPUT_ERROR);
        }
        int pageNumber = Integer.parseInt(pageNum);
        int size = Integer.parseInt(pageSize);
        //查询指定分页的内容
        List<LogsInfo> list = logsInfoDao.listLogsInfoByTimeAndName(username,pageNumber,size,fromTime,toTime);
        JSONArray json = JSONArray.fromObject(list);
        if(json.isEmpty()){
            return JsonUtil.failedJson(CommonValue.JSON_QUERY_FAILED);
        }else {
            System.out.println(json);
            return JsonUtil.successJson(CommonValue.JSON_QUERY_SUCCESS, null, json);
        }
    }

    /**
     * 查询时间范围内、所有操作员、某一页的日志信息
     * @param pageNum
     * @param pageSize
     * @param fromTime
     * @param toTime
     * @return
     */
    public String findLogsByTime(String pageNum, String pageSize,String fromTime,String toTime){
        if((fromTime == null || fromTime.isEmpty()) ||(toTime == null || toTime.isEmpty()) ||
                (pageNum == null || pageNum.isEmpty()) || (pageSize ==null || pageSize.isEmpty())){
            return JsonUtil.failedJson(CommonValue.JSON_INPUT_ERROR);
        }
        int pageNumber = Integer.parseInt(pageNum);
        int size = Integer.parseInt(pageSize);
        //查询指定分页的内容
        List<LogsInfo> list = logsInfoDao.listLogsInfoByTime(pageNumber,size,fromTime,toTime);
        JSONArray json = JSONArray.fromObject(list);
            if(json.isEmpty()){
                return JsonUtil.failedJson(CommonValue.JSON_QUERY_FAILED);
        }else {
            System.out.println(json);
            return JsonUtil.successJson(CommonValue.JSON_QUERY_SUCCESS, null, json);
        }
    }

    public String findLogsByCond(String username,String pageNum,String pageSize,String fromTime,String toTime){
        if((pageNum == null || pageNum.isEmpty()) || (pageSize ==null || pageSize.isEmpty())){
            return JsonUtil.failedJson(CommonValue.JSON_INPUT_ERROR);
        }
        int pageNumber = Integer.parseInt(pageNum);
        int size = Integer.parseInt(pageSize);

        //查询指定分页的内容
        List<LogsInfo> list = logsInfoDao.listLogsInfoByCond(username,pageNumber,size,fromTime,toTime);
        JSONArray json = JSONArray.fromObject(list);
        if(json.isEmpty()){
            return JsonUtil.failedJson(CommonValue.JSON_QUERY_FAILED);
        }else {
            //System.out.println(json);
            return JsonUtil.successJson(CommonValue.JSON_QUERY_SUCCESS, null, json);
        }
    }


    //查询错误日志，告警日志的分页
    public String listLogInfo(String username,String pageNum,String pageSize ,String fromTime,String toTime,Class className,String collectionName){

        if((pageNum == null || pageNum.isEmpty()) || (pageSize ==null || pageSize.isEmpty())){
            return JsonUtil.failedJson(CommonValue.JSON_INPUT_ERROR);
        }
        List<LogsInfo> errorLogInfoList = logsInfoDao.listLogInfoByCond(
                username,
                Integer.parseInt(pageNum),
                Integer.parseInt(pageSize),
                fromTime,
                toTime,
                className,
                collectionName
        );
        JSONArray json = JSONArray.fromObject(errorLogInfoList);
        if(json.isEmpty()){
            return JsonUtil.failedJson(CommonValue.JSON_QUERY_NULL);
        }else {
            return JsonUtil.successJson(CommonValue.JSON_QUERY_SUCCESS, null, json);
        }

    }

}
